home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / startrek / trek73 / src / dist.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  3KB  |  113 lines

  1. /*
  2.  * TREK73: dist.c
  3.  *
  4.  * Power distribution routines
  5.  *
  6.  * distribute
  7.  *
  8.  */
  9.  
  10. #include "defines.h"
  11. #include "structs.h"
  12. #include <math.h>
  13.  
  14. extern char engineer[];
  15.  
  16. distribute(sp)
  17. struct ship *sp;
  18. {
  19.     extern char title[];
  20.     register int i;
  21.     register int fuel;
  22.     register int load;
  23.     register int effload;
  24.     register int drain;
  25.     float shield;
  26.     extern char shutup[];
  27.     extern struct ship *shiplist[];
  28.  
  29.     fuel = sp->energy + (int)(sp->regen * 2);
  30.     /*
  31.      * Calculate negative phaser drains
  32.      */
  33.     for (i=0; i<4; i++) {
  34.         load = sp->phasers[i].load;
  35.         drain = sp->phasers[i].drain;
  36.         if ((sp->phasers[i].status & P_DAMAGED) || (drain >= 0)
  37.             || (sp->phasers[i].load <= 0))
  38.             continue;
  39.         /*
  40.          * Drain the lesser of either the current load if the
  41.          * load is less than the drain, or the drain value
  42.          */
  43.         if (drain < 0) {
  44.             effload = max(load + drain, 0);
  45.             fuel += load - effload;
  46.             sp->phasers[i].load = effload;
  47.         }
  48.     }
  49.     /*
  50.      * Calculate shield drains
  51.      */
  52.     shield = 0.0;
  53.     for (i=0; i<4; i++)
  54.         shield += sp->shields[i].attemp_drain;
  55.     drain = ceil((double) shield);
  56.     /*
  57.      * If all attempted drains are zero, or we have no
  58.      * fuel, our shields are down!
  59.      */
  60.     if (((shield == 0) || (fuel == 0)) && !shutup[SHIELDSF] && sp == shiplist[0]) {
  61.         printf("%s: %s, our shields are down!\n",engineer, title);
  62.         shutup[SHIELDSF]++;
  63.     }
  64.     /*
  65.      * If there's not enough fuel to sustain the drains, then
  66.      * ration it out in proportion to the attempted drains and
  67.      * say that shields are fluctuating.
  68.      */
  69.     if (drain <= fuel) {
  70.         fuel -= drain;
  71.         for (i=0; i<4; i++)
  72.             sp->shields[i].drain = sp->shields[i].attemp_drain;
  73.     } else {
  74.         if (!shutup[SHIELDSF] && sp == shiplist[0]) {
  75.             printf("%s: %s, our shields are fluctuating!\n", engineer, title);
  76.             shutup[SHIELDSF]++;
  77.         }
  78.         for (i=0; i<4; i++)
  79.             if (!sp->shields[i].attemp_drain)
  80.                 sp->shields[i].drain = sp->shields[i].attemp_drain * (float) fuel / drain;
  81.             else
  82.                 sp->shields[i].drain = 0;
  83.         fuel = 0;
  84.     }
  85.     /*
  86.      * Calculate positive phaser drains
  87.      */
  88.     for (i=0; i<4 && fuel > 0; i++) {
  89.         if (fuel <=0)
  90.             break;
  91.         load = sp->phasers[i].load;
  92.         drain = sp->phasers[i].drain;
  93.         if ((sp->phasers[i].status & P_DAMAGED) || load >= 10 || drain <= 0)
  94.             continue;
  95.         /*
  96.          * Load phasers either enough to top them off, or
  97.          * the full drain
  98.          */
  99.         if (drain > 0) {
  100.             effload = min(10, load + min(drain, fuel));
  101.             fuel -= effload - load;
  102.             sp->phasers[i].load = effload;
  103.         }
  104.     }
  105.     /*
  106.      * Now balance the level of energy with the numer of pods
  107.      */
  108.     if (fuel > sp->pods)
  109.         sp->energy = sp->pods;
  110.     else 
  111.         sp->energy = fuel;
  112. }
  113.